// 今日热点搜索框下的
import React, { Component } from 'react'
import { View,Text,Image  } from '@tarojs/components'
import {AtSearchBar,AtList, AtListItem} from 'taro-ui'
import Taro from '@tarojs/taro'
import TabBar from '../common/tabBar';
import './hostIndex.less'
import Host from './host'
import HostText from './hostText'

class hostIndex extends Component{
  componentDidMount() {
    Taro.request({
        url: 'http://localhost:9090/getAllHost',
        header: {
            'content-type': 'application/json' // 默认值
        },
        method: 'GET',
        dataType: 'json',
        success: (res) => {
            this.setState({
                hotSpotMedicineList: res.data
            });
        },
        fail: function (errMsg) {
            Taro.showToast({
                title: '服务器请求错误',
                icon: 'none',
                duration: 3000
            })
        }
    })
}

    constructor(props){
        super(props),
        this.state={
            hotSpotList: [],
            // 中药常识
            hotSpotMedicineList: [],
            // 热点数据类型
            chooseDataType: '',
            pageNum: '',
            pageSize: 10,
            // 输入框关键字
            keyword: '',
            newPageDataType: ''
        }
    }

    beforeReloadList(dataType, pageNum) {
        this.reloadList(dataType, pageNum)
  }
    reloadList(dataType,pageNum){
        Taro.request({
            url:'http://localhost:9090/getHost',
            data: {
              id: dataType,
              },
            header: {
                'content-type': 'application/json' // 默认值
            },
            method: 'GET',
            dataType: 'json',
            success: (res) => {
                this.setState({
                    hotSpotMedicineList: res.data
                });
            },
            fail: function (errMsg) {
              Taro.showToast({
                  title: '服务器请求错误',
                  icon: 'none',
                  duration: 3000
              })
          },
        })
    }
      Change(keyword){
        this.setState({
          keyword:keyword
        })
      }
      doSearch=(e)=>{
        e.preventDefault();//按钮提交的时候需要判断
        Taro.request({
          url:'http://localhost:9090/selectHost',
          data:{
            name:this.state.keyword
          },
          header: {
            'content-type': 'application/json' // 默认值
        },
        method: 'GET',
        dataType: 'json',
        success: (res) => {
            this.setState({
                hotSpotMedicineList: res.data
            });
        },
        fail: function (errMsg) {
          Taro.showToast({
              title: '服务器请求错误',
              icon: 'none',
              duration: 3000
          })
        },
        })
      }
    render(){
        return(
            <View>
                <AtSearchBar
                actionName='搜索'
                placeholder="搜索今日热点"
                value={this.state.keyword}
                onChange={this.Change.bind(this)}
                onActionClick={this.doSearch.bind(this)}
                className='search-input'
                />
                <Host parent={this}/>
                <HostText parent={this} hotSpotMedicineList={this.state.hotSpotMedicineList}/>
                {/*<TabBar tabBarCurrent={1}/>*/}
            </View>
        )
    }
}
hostIndex.defaultProps = {
    parent: null
}
export default hostIndex;
